package introsemp;
import java.sql.*;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * zChangePass.java
 *
 * Created on Nov 1, 2011, 8:38:59 AM
 */
/**
 *
 * @author G
 */
public class Form_ChangePassword extends javax.swing.JFrame {

    /** Creates new form zChangePass */
    Form_MainMenu menu;
    public Form_ChangePassword(Form_MainMenu menuGet) {
        initComponents();
        menu=menuGet;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        ConfirmationDialog = new javax.swing.JDialog();
        AreYouSure = new javax.swing.JLabel();
        Yes = new javax.swing.JButton();
        No = new javax.swing.JButton();
        ChangePassword = new javax.swing.JLabel();
        OldPassword = new javax.swing.JLabel();
        NewPassword = new javax.swing.JLabel();
        Change = new javax.swing.JButton();
        RepeatPassword = new javax.swing.JLabel();
        Cancel = new javax.swing.JButton();
        jPasswordField1 = new javax.swing.JPasswordField();
        jPasswordField2 = new javax.swing.JPasswordField();
        jPasswordField3 = new javax.swing.JPasswordField();

        ConfirmationDialog.setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        ConfirmationDialog.setTitle("Are You Sure?");
        ConfirmationDialog.setAlwaysOnTop(true);
        ConfirmationDialog.setMinimumSize(new java.awt.Dimension(386, 117));
        ConfirmationDialog.setResizable(false);

        AreYouSure.setFont(new java.awt.Font("Tahoma", 0, 14));
        AreYouSure.setForeground(new java.awt.Color(131, 22, 22));
        AreYouSure.setText("Are you sure you want to change your old password?");

        Yes.setForeground(new java.awt.Color(221, 29, 29));
        Yes.setText("YES");
        Yes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                YesActionPerformed(evt);
            }
        });

        No.setText("NO");
        No.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout ConfirmationDialogLayout = new javax.swing.GroupLayout(ConfirmationDialog.getContentPane());
        ConfirmationDialog.getContentPane().setLayout(ConfirmationDialogLayout);
        ConfirmationDialogLayout.setHorizontalGroup(
            ConfirmationDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ConfirmationDialogLayout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(ConfirmationDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, ConfirmationDialogLayout.createSequentialGroup()
                        .addGap(209, 209, 209)
                        .addComponent(Yes, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(No, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(AreYouSure))
                .addContainerGap(35, Short.MAX_VALUE))
        );
        ConfirmationDialogLayout.setVerticalGroup(
            ConfirmationDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ConfirmationDialogLayout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(AreYouSure)
                .addGap(18, 18, 18)
                .addGroup(ConfirmationDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Yes, javax.swing.GroupLayout.DEFAULT_SIZE, 48, Short.MAX_VALUE)
                    .addComponent(No, javax.swing.GroupLayout.DEFAULT_SIZE, 48, Short.MAX_VALUE))
                .addContainerGap())
        );

        setMinimumSize(new java.awt.Dimension(333, 223));
        setResizable(false);

        ChangePassword.setFont(new java.awt.Font("Tahoma", 0, 18));
        ChangePassword.setText("Change Password");

        OldPassword.setFont(new java.awt.Font("Tahoma", 0, 14));
        OldPassword.setText("Old Password:");

        NewPassword.setFont(new java.awt.Font("Tahoma", 0, 14));
        NewPassword.setText("New Password:");

        Change.setText("Ok");
        Change.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ChangeActionPerformed(evt);
            }
        });

        RepeatPassword.setFont(new java.awt.Font("Tahoma", 0, 14));
        RepeatPassword.setText("Confirm Password:");

        Cancel.setText("Cancel");
        Cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelActionPerformed(evt);
            }
        });

        jPasswordField2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jPasswordField2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(NewPassword)
                    .addComponent(RepeatPassword)
                    .addComponent(OldPassword))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(Change)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Cancel))
                    .addComponent(jPasswordField3, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jPasswordField1, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jPasswordField2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE)))
                .addContainerGap(38, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(103, Short.MAX_VALUE)
                .addComponent(ChangePassword)
                .addGap(91, 91, 91))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ChangePassword)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(OldPassword))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jPasswordField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(NewPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jPasswordField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(RepeatPassword))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Cancel, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Change, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(23, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void CancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelActionPerformed

        this.dispose();
    }//GEN-LAST:event_CancelActionPerformed

    private void ChangeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ChangeActionPerformed

        //Checking
        this.ConfirmationDialog.setVisible(true);
        this.ConfirmationDialog.setSize(386,150);
    }//GEN-LAST:event_ChangeActionPerformed

    private void NoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NoActionPerformed

        this.ConfirmationDialog.dispose();
        this.dispose();
    }//GEN-LAST:event_NoActionPerformed

private void jPasswordField2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jPasswordField2ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jPasswordField2ActionPerformed

private void YesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_YesActionPerformed
// TODO add your handling code here:
    String pass="";
     try{
        PreparedStatement pstmt= menu.con.prepareStatement("SELECT * FROM security");
        ResultSet allTerminals= pstmt.executeQuery();
       while(allTerminals.next()){
            pass=allTerminals.getString("Password");
        } 
        }
        catch(SQLException ex){
            System.out.println("nada");
        }
     if(jPasswordField1.getText().equals(pass)){
     if(jPasswordField2.getText().equals(jPasswordField3.getText())){
     try{
        PreparedStatement pstmt2= menu.con.prepareStatement("UPDATE security SET Password=? WHERE Password=?"); 
        int i=1;
        pstmt2.setString(i++,jPasswordField2.getText());
        pstmt2.setString(i++,jPasswordField1.getText());
        pstmt2.executeUpdate();
        }
        catch(SQLException ex){
            System.out.println("nada");
        }
     this.ConfirmationDialog.dispose();
     this.setVisible(false);
     }
     }
}//GEN-LAST:event_YesActionPerformed

    /**
     * @param args the command line arguments
     */
 
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        
        //</editor-fold>

        /* Create and display the form */
      
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel AreYouSure;
    private javax.swing.JButton Cancel;
    private javax.swing.JButton Change;
    private javax.swing.JLabel ChangePassword;
    private javax.swing.JDialog ConfirmationDialog;
    private javax.swing.JLabel NewPassword;
    private javax.swing.JButton No;
    private javax.swing.JLabel OldPassword;
    private javax.swing.JLabel RepeatPassword;
    private javax.swing.JButton Yes;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JPasswordField jPasswordField2;
    private javax.swing.JPasswordField jPasswordField3;
    // End of variables declaration//GEN-END:variables
}
